﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
namespace demo_ver2
{
    public class MyParagraph
    {
        private double fontSize = 12;
        private int tabIndent = 0;

        public int TabIndent
        {
            get { return tabIndent; }
            set { tabIndent = value; }
        }
        public double FontSize
        {
            get { return fontSize; }
            set { fontSize = value/100; }
        }


        private List<MyRow> listRow;

        internal List<MyRow> ListRow
        {
            get { return listRow; }
            set { listRow = value; }
        }

        public MyParagraph()
        {
            this.listRow = new List<MyRow>();
        }

        public void addRow(MyRow _row)
        {
            if (this.listRow != null)
            {
                this.listRow.Add(_row);
            }
        }

        public int rowCount()
        {
            return this.listRow.Count;
        }

        public void BindTextToControl(RichTextBox richTextBox)
        {
            
            for (int i = 0; i < this.listRow.Count; i++)
            {
                for (int j = 0; j < this.tabIndent; j++)
                {
                    richTextBox.AppendText("\t");
                }
                richTextBox.AppendText(listRow[i].Text);
                
               // ponter1 = richTextBox.Document.ContentEnd;
                var range = FindWordFromPosition(richTextBox.Document.ContentStart, listRow[i].Text);
                if (listRow[i].FontSize < 0)
                    range.ApplyPropertyValue(TextElement.FontSizeProperty, this.FontSize);
                else
                    range.ApplyPropertyValue(TextElement.FontSizeProperty, listRow[i].FontSize);

            }
            richTextBox.AppendText("\n");
            
                
        }

        TextRange FindWordFromPosition(TextPointer position, string word)
        {
            while (position != null)
            {
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    string textRun = position.GetTextInRun(LogicalDirection.Forward);

                    // Find the starting index of any substring that matches "word".
                    int indexInRun = textRun.IndexOf(word);
                    if (indexInRun >= 0)
                    {
                        TextPointer start = position.GetPositionAtOffset(indexInRun);
                        TextPointer end = start.GetPositionAtOffset(word.Length);
                        return new TextRange(start, end);
                    }
                }

                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }

            // position will be null if "word" is not found.
            return null;
        }
    }
}
